
/**
 * 该文件为计算七牛云【图片上传】携带的token，最终返回token
 */

//  ----------------------图片----------------------------
var newPolicy = function (prefix, suffix) {
  return {
    "scope": "users",
    "saveKey": prefix + "$(etag)" + suffix,
    "deadline": Math.round(new Date().getTime() / 1000) + 3600
  };
}

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
                                                                                                                                                                                                      
// 编码
function base64encode(str) {
  var out, i, len;
  var c1, c2, c3;
  len = str.length;
  i = 0;
  out = "";
  while (i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if (i == len) {
      out += base64EncodeChars.charAt(c1 >> 2);
      out += base64EncodeChars.charAt((c1 & 0x3) << 4);
      out += "==";
      break;
    }
    c2 = str.charCodeAt(i++);
    if (i == len) {
      out += base64EncodeChars.charAt(c1 >> 2);
      out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
      out += base64EncodeChars.charAt((c2 & 0xF) << 2);
      out += "=";
      break;
    }
    c3 = str.charCodeAt(i++);
    out += base64EncodeChars.charAt(c1 >> 2);
    out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
    out += base64EncodeChars.charAt(c3 & 0x3F);
  }
  return out;
}


function b64_hmac_sha1(k, d, _p, _z) {
  // heavily optimized and compressed version of http://pajhome.org.uk/crypt/md5/sha1.js
  // _p = b64pad, _z = character size; not used here but I left them available just in case
  if (!_p) {
    _p = '=';
  }
  if (!_z) {
    _z = 8;
  }

  function _f(t, b, c, d) {
    if (t < 20) {
      return (b & c) | ((~b) & d);
    }
    if (t < 40) {
      return b ^ c ^ d;
    }
    if (t < 60) {
      return (b & c) | (b & d) | (c & d);
    }
    return b ^ c ^ d;
  }

  function _k(t) {
    return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
  }

  function _s(x, y) {
    var l = (x & 0xFFFF) + (y & 0xFFFF),
      m = (x >> 16) + (y >> 16) + (l >> 16);
    return (m << 16) | (l & 0xFFFF);
  }

  function _r(n, c) {
    return (n << c) | (n >>> (32 - c));
  }

  function _c(x, l) {
    x[l >> 5] |= 0x80 << (24 - l % 32);
    x[((l + 64 >> 9) << 4) + 15] = l;
    var w = [80],
      a = 1732584193,
      b = -271733879,
      c = -1732584194,
      d = 271733878,
      e = -1009589776;
    for (var i = 0; i < x.length; i += 16) {
      var o = a,
        p = b,
        q = c,
        r = d,
        s = e;
      for (var j = 0; j < 80; j++) {
        if (j < 16) {
          w[j] = x[i + j];
        } else {
          w[j] = _r(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
        }
        var t = _s(_s(_r(a, 5), _f(j, b, c, d)), _s(_s(e, w[j]), _k(j)));
        e = d;
        d = c;
        c = _r(b, 30);
        b = a;
        a = t;
      }
      a = _s(a, o);
      b = _s(b, p);
      c = _s(c, q);
      d = _s(d, r);
      e = _s(e, s);
    }
    return [a, b, c, d, e];
  }

  function _b(s) {
    var b = [],
      m = (1 << _z) - 1;
    for (var i = 0; i < s.length * _z; i += _z) {
      b[i >> 5] |= (s.charCodeAt(i / 8) & m) << (32 - _z - i % 32);
    }
    return b;
  }

  function _h(k, d) {
    var b = _b(k);
    if (b.length > 16) {
      b = _c(b, k.length * _z);
    }
    var p = [16],
      o = [16];
    for (var i = 0; i < 16; i++) {
      p[i] = b[i] ^ 0x36363636;
      o[i] = b[i] ^ 0x5C5C5C5C;
    }
    var h = _c(p.concat(_b(d)), 512 + d.length * _z);
    return _c(o.concat(h), 512 + 160);
  }

  function _n(b) {
    var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
      s = '';
    for (var i = 0; i < b.length * 4; i += 3) {
      var r = (((b[i >> 2] >> 8 * (3 - i % 4)) & 0xFF) << 16) | (((b[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8) | ((b[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF);
      for (var j = 0; j < 4; j++) {
        if (i * 8 + j * 6 > b.length * 32) {
          s += _p;
        } else {
          s += t.charAt((r >> 6 * (3 - j)) & 0x3F);
        }
      }
    }
    return s;
  }

  function _x(k, d) {
    return _n(_h(k, d));
  }
  return _x(k, d);
}
//  ----------------------图片----------------------------

/**
 * 七牛云【上传】携带的token
 * params: {
 * type: 【img: 图片】 【audio: 音频】 
 * }
 */
export const getQiNiuToken = () => {
  var userId = '5aacd5fa8b77613b60e9b78e';
  var urlPrefix = userId + "/img/";
  var urlSuffix = '';
  var dotIdx = name.lastIndexOf('.');
  if (dotIdx > 0) {
    urlSuffix = name.substr(dotIdx);
  }

  var secret = "ewy219lrC-dScQss4nY96cvyoIAxIoAat5Vc_yH3";
  var access = "hkqZpCDJI6_lN4tqlvSNdcN2TlqQROyLKKQEgpAS";

  var raw = newPolicy(urlPrefix, urlSuffix);
  var policy = base64encode(JSON.stringify(raw));
  var sign = b64_hmac_sha1(secret, policy).replace(/\//g, '_').replace(/\+/g, '-');
  var token = access + ":" + sign + ":" + policy;
  return token;
}
// ---------------------音频-------------------------
var newPolicyMp3 = function (prefix, suffix) {
	return {
		"scope": "users-pub",
		"saveKey": prefix + "$(etag)" + suffix,
		"deadline": Math.round(new Date().getTime() / 1000) + 3600
	};
}
// ---------------------音频-------------------------

/**
 * 七牛云【上传】携带的token
 * params: {
 * type: 【img: 图片】 【audio: 音频】 
 * }
 */
export const getAudioToken = function () {

	var userId = '5aacd5fa8b77613b60e9b78e';
	var urlPrefix = userId + '/' + "audio/";
	var urlSuffix = '.mp3';

	var secret = "ewy219lrC-dScQss4nY96cvyoIAxIoAat5Vc_yH3";
	var access = "hkqZpCDJI6_lN4tqlvSNdcN2TlqQROyLKKQEgpAS";

	var raw = newPolicyMp3(urlPrefix, urlSuffix);
	var policy = base64encode(JSON.stringify(raw));
	var sign = b64_hmac_sha1(secret, policy).replace(/\//g, '_').replace(/\+/g, '-');
	var token = access + ":" + sign + ":" + policy;

	return token;
}
